package com.ruoyi.shop.common.utils;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.shop.modules.domain.vo.ProductVo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-02  15:41
 * @Description: 实体转换工具类
 */
public class BeanConvertUtils {
    /**
     * 通用方法，用于将Product或EsProduct列表转换为ProductVo列表
     *
     * @param products 源对象列表
     * @param albumPicsExtractor 用于从源对象中提取相册图片的函数
     * @param <T> 源对象的类型
     * @return 转换后的ProductVo列表
     */
    public static <T> List<ProductVo> convertProductsToVos(List<T> products, Function<T, String> albumPicsExtractor) {
        List<ProductVo> vos = new ArrayList<>();
        for (T product : products) {
            ProductVo vo = new ProductVo();
            String albumPics = albumPicsExtractor.apply(product);
            if (albumPics != null && !albumPics.isEmpty()) {
                List<String> pics = Arrays.asList(albumPics.split(",", 5));
                vo.setAlbumPics(pics);
            }
            BeanUtils.copyProperties(product, vo);
            vos.add(vo);
        }
        return vos;
    }
}
